package com.ywb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ywb.entity.EduCourse;
import com.ywb.entity.EduCourseDescription;
import com.ywb.entity.vo.CourseConditionVo;
import com.ywb.entity.vo.CourseInfoVo;
import com.ywb.mapper.EduCourseMapper;
import com.ywb.service.EduCourseDescriptionService;
import com.ywb.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author ywb
 * @since 2021-05-27
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService descriptionService;
    @Override
    public void saveCourseInfo(CourseInfoVo courseInfoVo) {
        EduCourse course = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,course);
        save(course);

        EduCourseDescription description = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoVo, description);
//        课程描述和课程的id应该是一样的
        description.setId(course.getId());
        descriptionService.save(description);
    }

    @Override
    public Page<EduCourse> getPageByCourseCondition(CourseConditionVo condition, long current, long limit) {
        String courseStatus = condition.getCourseStatus();
        String courseTitle = condition.getCourseTitle();
        String courseSubject = condition.getCourseSubject();
        String courseTeacher = condition.getCourseTeacher();

        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if(StringUtils.hasLength(courseStatus)){
            wrapper.eq("status", courseStatus);
        }
        if(StringUtils.hasLength(courseTitle)){
            wrapper.like("title", courseTitle);

        }
        if(StringUtils.hasLength(courseSubject)){
            wrapper.eq("subject_parent_id", courseSubject);

        }
        if(StringUtils.hasLength(courseTeacher)){
            wrapper.eq("teacher_id", courseTeacher);
        }

        Page<EduCourse> page = new Page<>(current, limit);
        page(page, wrapper);

        return page;
    }
}
